package mythesis.gui.main;
import mythesis.gui.main.components.MenuBar;
import mythesis.gui.main.components.EventBar;
import mythesis.gui.main.components.Background;
import mythesis.gui.main.components.TilePanel;
import mythesis.gui.main.components.EditorPanel;
import mythesis.gui.main.components.Notification;
import mythesis.gui.main.components.api.Tile;
import mythesis.tiles.character.*;
import mythesis.tiles.variable.*;
import mythesis.controller.*;
import mythesis.gui.popup.InputPopup;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.text.*;
import javafx.scene.input.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.animation.transition.*;
import javafx.scene.control.*;
//import javafx.ext.swing.*;


var screenWidth  = 960;
var screenHeight = 540;

var inputLine = -1;					// Line of tile to target for input text change.  If -1, input mode is OFF.
var inputArg  = -1;					// Argument of tile to target for input text change.

var fileNumber:Number = 1;					// When saving/loading, specifies which CustomAIx file.

var tileDragX:Integer   = 0;
var tileDragY:Integer   = 0;
var tileDragXb:Integer  = 0;
var tileDragYb:Integer  = 0;
var inputPopupX:Integer = 0;
var inputPopupY:Integer = 0;
var inputPopupXb:Integer = 0;
var inputPopupYb:Integer = 0;

var vm          = new VariableManager;
var xmltp       = new XMLTileParser;
var xmljp       = new XMLJavaParser;
var menuBar     = new MenuBar;
var eventBar = EventBar
{
    isDragging: bind (TileDragGroupContent.content.size()!=0)
}


var editorPanel = EditorPanel
{
    
    isDragging: bind (TileDragGroupContent.content.size()==0)
	page: bind eventBar.event;	
}
var tilePanel   = TilePanel
{
	banList : TileBan.getTileBanList(editorPanel.eventList[editorPanel.page]);
}

var tilePickup = new TilePickup;
var tileDrop   = new TileDrop;
//var codeReader = new CodeReader;
var notification = new Notification;

var screenDark = Rectangle
{
	width   : screenWidth
    height  : screenHeight
    fill    : Color.BLACK
    opacity : 0
}

var NotificationGroup = Group
{
    translateX: 0
    translateY: 490
    opacity   : 0
    content:
    [
    	Notification
    	{
    	    text:  "default notification"
    	}
    ]
}

var tranDarken = FadeTransition
{
    duration  : 0.2s
    node      : screenDark 
    fromValue : 0.0
    toValue   : 0.65
    repeatCount: 1 
}
var tranInvis = FadeTransition
{
    duration  : 0.2s
    node      : screenDark 
    fromValue : 0.65
    toValue   : 0.0
    repeatCount: 1 
}

var tranFadeAway = FadeTransition
{
    duration  : 15.0s
    node      : NotificationGroup
    fromValue : 1.0
    toValue   : 0.0
    repeatCount : 1
}



var ScreenDarkGroup = Group
{
    content:
    [
		screenDark
    ]
    
}

var InputPopupGroup = Group
{
	content: []
	translateX: bind inputPopupX;
	translateY: bind inputPopupY;
	onMousePressed: function(e: MouseEvent): Void
	{
	    inputPopupXb = e.x as Integer;
	    inputPopupYb = e.y as Integer;
	    
	}
	onMouseDragged: function(e: MouseEvent): Void
	{
	    var myX = e.sceneX-inputPopupXb as Integer;
	    var myY = e.sceneY-inputPopupYb as Integer;
	    if (myX > 290 and myX < 650)
	    {
	    	inputPopupX = myX;
	    }
	    if (myY > 75 and myY < 300)
	    {
	       	inputPopupY = myY;
	    }
	    
	}
}

var TileDragGroupContent = Group
{
    content: [ ]
}

var TileDragGroup = Group
{
    content: [ TileDragGroupContent ]
    //translateX: bind tileDragX-270
    translateX: bind tileDragX - 25
    translateY: bind tileDragY - 10
    
    onMouseMoved: function (e:MouseEvent):Void
    {
        tileDragX = e.sceneX as Integer;
        tileDragY = e.sceneY as Integer;
    }
    
    onMousePressed: function (e:MouseEvent):Void
    {
        if (e.secondaryButtonDown)
        {
            delete TileDragGroupContent.content[0];
        }
    }
}



var MenuBarGroup = Group
{
    content: [ menuBar ]
    translateX: 10;
    translateY: 10;
    onMousePressed: function (e:MouseEvent):Void{
        if (inputLine == -1)
        {
            // New
			menuBar.group.content[1].
				onMouseClicked = function(e:MouseEvent):Void
				 {
				     notifyUser(editorPanel.deleteAllTiles());
				     for (i in [0..editorPanel.eventList.size()-1])
	   			     {
	   			     	vm.setUserVariables_Number(editorPanel.eventList[i], []);
	   			     	vm.setUserVariables_Word(editorPanel.eventList[i], []);
	   			     	vm.setUserVariables_Truth(editorPanel.eventList[i], []);
	   			     	vm.setCandidateVariables(editorPanel.eventList[i], []);	   			         
	   			     }
				 }
			// Open
			menuBar.group.content[2].
				onMouseClicked = function(e:MouseEvent):Void
	   			 {
	   			     //println("Open");
	   			     for (i in [0..editorPanel.eventList.size()-1])
	   			     {
	   			     	var tl:Group;
	   			     	xmltp.newTileList(editorPanel.eventList[i]);
	   			     	tl = xmltp.getTileList(fileNumber);
	   			     	var nums = xmltp.numList;
	   			     	var words = xmltp.wordList;
	   			     	var truths = xmltp.truthList;
	   			     	var cands = xmltp.candidateList;
	   			     	editorPanel.listAll[i].content = tl.content;
	   			     	vm.setUserVariables_Number(editorPanel.eventList[i], nums);
	   			     	vm.setUserVariables_Word(editorPanel.eventList[i], words);
	   			     	vm.setUserVariables_Truth(editorPanel.eventList[i], truths);
	   			     	vm.setCandidateVariables(editorPanel.eventList[i], cands);
	   			     	//println("size0 {editorPanel.listAll[0].content.size()}");
	   			     	//println("size1 {editorPanel.listAll[1].content.size()}");
	   			     }
	   			     notifyUser("Opened CustomAI{fileNumber}.xml");
	   			 }
	   		// Save
			menuBar.group.content[3].
				onMouseClicked = function(e:MouseEvent):Void
				 {
				     var xmlFile = "<?xml version=\"1.0\"?>\n";
				     xmlFile    += "<ai>\n";
				     for (i in [0..editorPanel.eventList.size()-1])
				     {
				     	xmlFile    += CodeReader.read(editorPanel.listAll[i].content, editorPanel.eventList[i], vm);
				     }
				     
				     xmlFile    += "</ai>";
				     //println(xmlFile);
				     notifyUser(WriteXML.write(xmlFile, fileNumber));
				     //MenuSave.save(editorPanel.listAll[0].content);
				 }
			// Compile
			menuBar.group.content[4].
				onMouseClicked = function(e:MouseEvent):Void
				 {
				     var allFull = true;
				     var sameCondEnd = true;
				     // check if all fields are full
				     for (i in [0..editorPanel.eventList.size()-1])
				     {
				         if (editorPanel.areAllFieldsFull(i) == false)
				         {
				             allFull = false;
				             notifyUser("ERROR:  There's a tile in {editorPanel.eventList[i]} that isn't full.  Aborting conversion to Java.");
				             break;
				         }
				     }
				     
				     // check the equal number of IF/WHILE vs END tiles
				     for (i in [0..editorPanel.eventList.size()-1])
				     {
				         if (editorPanel.areCondEndTilesEqual(i) == false)
				         {
				             sameCondEnd = false;
				             notifyUser("ERROR:  Uneven number of conditional vs end tiles in {editorPanel.eventList[i]}.  It will cause issues.  Aborting conversion to Java.");
				             break;
				         }
				     }
				     
				     
				     
				     // Write the Java file
				     if (allFull == true and sameCondEnd == true)
				     {
				     	notifyUser(WriteJava.write(xmljp.getAICode(fileNumber), fileNumber));
				     }
				 }
			// Run
			menuBar.group.content[5].
				onMousePressed = function(e:MouseEvent):Void
				 {
				    //println("Compile and Run");
				    notifyUser(CompilerFX.compile(fileNumber));
				    //CompilerFX.compile(fileNumber);
				    //notifyUser("Compiled file CustomAI{fileNumber as Integer}.java.");
				    
				    
				    //GameLauncher.startGame();
				 	//tranInvis.play();
				 	//tranDarken.stop();
				 }
			// CustomAI #1
			menuBar.group.content[6].
	    		onMousePressed = function(e:MouseEvent):Void
				 {
				    fileNumber = 1;
				    notifyUser("Working on CustomAI1.");
				 }
			// CustomAI #2
			menuBar.group.content[7].
	    		onMousePressed = function(e:MouseEvent):Void
				 {
				    fileNumber = 2;
				    notifyUser("Working on CustomAI2.");
				 }
			// CustomAI #3
	    	menuBar.group.content[8].
	    		onMousePressed = function(e:MouseEvent):Void
				 {
				    fileNumber = 3;
				    notifyUser("Working on CustomAI3.");
				 }
	    }
	}
}

var BackgroundGroup = Group
{
	var background = new Background;
    content: [ background ]
    translateX: 0
    translateY: 0
} 

var TilePanelGroup = Group
{
 	content: [ tilePanel ]
 	translateX: 10
 	translateY: 55
 	onMousePressed: function( e: MouseEvent): Void{
 	    
 	    if (inputLine == -1)
 	    {
			var tile = tilePickup.pickupTile(tilePanel.mouseClick);
	 	    insert tile into TileDragGroupContent.content;
	        //tileDragX = e.sceneX+150 as Integer;
	        //tileDragY = e.sceneY-10 as Integer;
	        tileDragX = e.sceneX as Integer;
	        tileDragY = e.sceneY as Integer;
	        
	        tilePanel.mouseClick = 0;
        }
 	}
}

var EventBarGroup = Group
{
 	content:
 	[
 		eventBar
 	]
 	translateX: 280
 	translateY: 10
 	
 	onMousePressed: function(e: MouseEvent): Void
 	{
 	    eventBar.toInputLine = inputLine;
 	    tilePanel.banList = TileBan.getTileBanList(editorPanel.eventList[editorPanel.page]);
 	    notifyUser(eventBar.eventDescription[editorPanel.page]);
 	}
}

var EditorPanelGroup = Group
{
 	content: bind
 	[
 		editorPanel
 	]
 	
 	translateX: 280
 	translateY: 55



	onMouseWheelMoved: function(e: MouseEvent): Void
	{
	    if (e.wheelRotation == -1.0)
	    {
   	        editorPanel.barOffsetY -= 5;
   		    if (editorPanel.barOffsetY < 0){ editorPanel.barOffsetY = 0; }
   		    if (editorPanel.barOffsetY > 290){ editorPanel.barOffsetY = 290; }
   		    editorPanel.barOffsetYPct = editorPanel.barOffsetY / 290;
	    }
	    else if (e.wheelRotation == 1.0)
	    {
   	        editorPanel.barOffsetY += 5;
  		    if (editorPanel.barOffsetY < 0){ editorPanel.barOffsetY = 0; }
   		    if (editorPanel.barOffsetY > 290){ editorPanel.barOffsetY = 290; }
   		    editorPanel.barOffsetYPct = editorPanel.barOffsetY / 290;	        
	    }
	}

	onMouseDragged: function(e: MouseEvent): Void
	{
	    //inputPopupX = e.sceneX-125 as Integer;
	    //inputPopupY = e.sceneY-95 as Integer;
	    //inputPopupX = e.sceneX-e.translateX as Integer;
	}

	onMousePressed: function(e: MouseEvent): Void
	{
	    // ------- Input Popup ------------------------------
	    // If left click
	    if (e.primaryButtonDown)
	    {
	        
	        //println("{inputLine} -- {editorPanel.isLineUsed()} -- {editorPanel.numArgClicked()} -- {InputPopupGroup.content.size()}");
	        // Not input mode, go to input mode!  Bring up input popup!
	        //println("pit inputLine == -1 and editorPanel.isLineUsed() {editorPanel.numArgClicked() != -1} {TileDragGroupContent.content.size()}");
        	inputArg  = editorPanel.numArgClicked();
	        println("BEF {inputLine} -- {inputArg} -- {editorPanel.isLineUsed()}");
	        if (inputLine == -1 and inputArg != -1 and editorPanel.isLineUsed() and editorPanel.numArgClicked() != -1 and TileDragGroupContent.content.size()==0)
	        {
	            println("AFT {inputLine} -- {inputArg} -- {editorPanel.isLineUsed()}");
	            //println("click");
	        	inputLine = editorPanel.lineClick;
	        	inputArg  = editorPanel.numArgClicked();
	        	var myX = e.sceneX as Integer;
	        	var myY = e.sceneY as Integer;
	        	if (myX > 650){ myX = 650; }
	        	if (myX < 290){ myX = 290; }
	        	if (myY > 300){ myY = 300; }
	        	if (myY < 75 ){ myY = 75;  }
			    inputPopupX = myX;
			    inputPopupY = myY;
			    
			    var ip:InputPopup = new InputPopup;
			    
			    var page=editorPanel.page;
			    //--- set number list
			    //println(" num list: {getIpVarList('Number')}");
			    ip.userVariables_Number = vm.getIpVarList("Number", editorPanel.page);
			    ip.userVariables_Word = vm.getIpVarList("Word", editorPanel.page);
			    ip.userVariables_Truth = vm.getIpVarList("Truth", editorPanel.page);
			    ip.specialVariables = editorPanel.getListOfSpecialVariables();	// Special variables, found inside each tile
			    
		        // insert detective pad variables
		        insert vm.getBonusVars("DetectivePad", editorPanel.page) into ip.userVariables_Truth;
		        
		        // insert bool funcs to check if AI has the suggested card
		        if (editorPanel.page == 3 or editorPanel.page == 4)
		        {
		        	insert vm.getBonusVars("CheckSugElim", editorPanel.page) into ip.userVariables_Truth;
		        }
			    
			    // if conditional tile, or if set tile and we clicked the second arg, insert correct special variables
			    //println(" YEAH {(editorPanel.canReadBonusVar(inputLine, inputArg))}");
			    if (editorPanel.isConditionTile(inputLine) or editorPanel.canReadBonusVar(inputLine, inputArg))
			    {
			        // input:  type, page
			        insert vm.getBonusVars("Number", editorPanel.page) into ip.userVariables_Number;
			        insert vm.getBonusVars("Word", editorPanel.page) into ip.userVariables_Word;
			        insert vm.getBonusVars("Truth", editorPanel.page) into ip.userVariables_Truth;
			        
			        // insert card list
			        insert vm.getBonusVars("MyCards", editorPanel.page) into ip.userVariables_Word;
			        
			        // insert room functions, such as finding nearest room and checking if AI is inside a room
			        insert vm.getBonusVars("RoomFuncs_Word", editorPanel.page) into ip.userVariables_Word;
			        insert vm.getBonusVars("RoomFuncs_GetRoom_Word", editorPanel.page) into ip.userVariables_Word;
			        insert vm.getBonusVars("RoomFuncs_Truth", editorPanel.page) into ip.userVariables_Truth;
			        
			        // insert funcs to retrieve a random card
			        insert vm.getBonusVars("GetRandCard", editorPanel.page) into ip.userVariables_Word;
			        
			        // insert the blank
			        insert "blank" into ip.userVariables_Word;
			        
			        // insert checks if user has the card
			        insert vm.getBonusVars("HaveCard", editorPanel.page) into ip.userVariables_Truth;
			        
			        // if we're disproving or listening, then give functions that enables
			        // the user to easily see if he has the suggested stuff
			    	if (editorPanel.page == 4 or editorPanel.page == 3)
			    	{
				        insert vm.getBonusVars("HaveSuggested", editorPanel.page) into ip.userVariables_Truth;
			    	}
			    }
			    
			    // if goto tile, insert rooms
			    if (editorPanel.isGoToTile(inputLine))
			    {
			        ip.userVariables_Word = [];
			        insert vm.getBonusVars("RoomList", editorPanel.page) into ip.userVariables_Word;
			        insert vm.getBonusVars("RoomFuncs_GetRoom_Word", editorPanel.page) into ip.userVariables_Word;
			    }
			    
			    if (editorPanel.isSuggestAccuseTile(inputLine))
			    {
			        ip.userVariables_Word = [];
			        if (inputArg==0)
			        {
						//insert vm.getBonusVars("SuggestAccuseSuspect", editorPanel.page) into ip.userVariables_Word;
						insert vm.getUserVariables_Word(editorPanel.eventList[editorPanel.page]) into ip.userVariables_Word;			            
			        }
			        else if (inputArg==1)
			        {
			         	//insert vm.getBonusVars("SuggestAccuseWeapon", editorPanel.page) into ip.userVariables_Word;
			         	insert vm.getUserVariables_Word(editorPanel.eventList[editorPanel.page]) into ip.userVariables_Word;
			         	   
			        }
			        else if (inputArg==2)
			        {
			            //insert vm.getBonusVars("SuggestAccuseRoom", editorPanel.page) into ip.userVariables_Word;
			            insert vm.getUserVariables_Word(editorPanel.eventList[editorPanel.page]) into ip.userVariables_Word;
			        }
			    }
			    // if we're inside disproving page
			    //if ((editorPanel.page == 4 or editorPanel.page == 3) and (editorPanel.canReadBonusVar(inputLine, inputArg) or (editorPanel.isConditionTile(inputLine))))
			    if (editorPanel.canReadBonusVar(inputLine, inputArg) or (editorPanel.isConditionTile(inputLine)))
			    {
			    	insert vm.getBonusVars("SuspectList", editorPanel.page) into ip.userVariables_Word;
			    	insert vm.getBonusVars("WeaponList", editorPanel.page) into ip.userVariables_Word;
			    	insert vm.getBonusVars("RoomList", editorPanel.page) into ip.userVariables_Word;
			    }
			    

			    
			    ip.updateLists();
			    
			    
			    //println(" special vars {editorPanel.getListOfSpecialVariables()}");
			    
			    ip.allowedDataTypes = editorPanel.getListOfAllowedDataTypes();
			    ip.allowedDataValue = editorPanel.getListOfAllowedDataValue();

				// if conditional tile, and we're working on the first argument,
				// then add special variables to it.

			    
			    // if it's a conditional tile and we're working on third arg (arg=2)
			    // base the allowed data types on the first arg type
			    //println(" iscond {editorPanel. isConditionTile(inputLine)}");
			    //println(" inputarg {inputArg}");

			    

			    
			    if (editorPanel.isConditionTile(inputLine) and inputArg==2)
			    {
			        // number
			        if (editorPanel.getCondFieldType1(inputLine)==1)
			        {
			            ip.allowedDataTypes = [false, false, true, false];
			        }
			        // word
			        else if (editorPanel.getCondFieldType1(inputLine)==2)
			        {
			            ip.allowedDataTypes = [false, true, false, false];
			        }
			        // truth
			        else if (editorPanel.getCondFieldType1(inputLine)==3)
			        {
			            ip.allowedDataTypes = [true, false, false, false];
			        } 
			    }
			    
			    if (editorPanel.isSetTile(inputLine) and inputArg==1)
			    {
			        // number
			        if (editorPanel.getSetFieldType1(inputLine)==1)
			        {
			            ip.allowedDataTypes = [false, false, true, false];
			        }
			        // word
			        else if (editorPanel.getSetFieldType1(inputLine)==2)
			        {
			            ip.allowedDataTypes = [false, true, false, false];
			        }
			        // truth
			        else if (editorPanel.getSetFieldType1(inputLine)==3)
			        {
			            ip.allowedDataTypes = [true, false, false, false];
			        } 
			    }

			    
			    //println("inputLine {inputLine}");
			    //println("inputArg  {inputArg}");
			    //println("types     {editorPanel.getListOfAllowedDataTypes()}");
			    //println("iptypes   {ip.allowedDataTypes}");
			    //println("values    {editorPanel.getListOfAllowedDataValue()}");
			    //println(editorPanel.getListOfAllowedDataValue());
			    ip.updateAllowables();
			    
			    ip.initSettings(editorPanel.getArgDataType(inputArg), editorPanel.getArgDataValue(inputArg), editorPanel.getArgText(inputLine, inputArg));
			    
			    insert ip into InputPopupGroup.content;
			    
			    
			    //tranDarken.play();
			    //tranInvis.stop();
			    //println(InputPopupGroup.content.size());
	        }
	        
	        // Input mode!
	        // -- Check if we try to change the text!
	        // -- Check if we try to close popup!
	        else
	        {
        	    // display notification if any -------------
        	    if ({InputPopupGroup.content[0] as InputPopup}.toNotify != "")
        	    {
        	        notifyUser({InputPopupGroup.content[0] as InputPopup}.toNotify);
        	        {InputPopupGroup.content[0] as InputPopup}.toNotify = "";
        	    }
	        	// Check attempt to close
	        	if ({InputPopupGroup.content[0] as InputPopup}.toClose)
	        	{
	        	    delete InputPopupGroup.content[0];
	        	    editorPanel.setNumArgClicked(-1);
	        	    inputArg  = -1;
	        	    inputLine = -1;
				 	//tranInvis.play();
				 	//tranDarken.stop();
	        	}
	        	// Check attempt to input
	        	else if ({InputPopupGroup.content[0] as InputPopup}.toAccept)
	        	{
	        	    // update tile arg text --------------------
	        	    var inputDataValue = {InputPopupGroup.content[0] as InputPopup}.selectedDataValue;
	        	    var strDataText:String;
	        	    
	        	    if (inputDataValue == 1){		// constant
	        	    	strDataText = {InputPopupGroup.content[0] as InputPopup}.textInput;
	        	    }
	        	    else if (inputDataValue == 2)	// variable name
	        	    {
	        	        strDataText = {InputPopupGroup.content[0] as InputPopup}.selectedVariable;
	        	    }
	        	    else if (inputDataValue == 3)   // special variable
	        	    {
	        	        strDataText = {InputPopupGroup.content[0] as InputPopup}.selectedSpecial;
	        	    }
	        	    var strDataType:String = {InputPopupGroup.content[0] as InputPopup}.selectedType.toString();
	        	    var strDataValue:String = {InputPopupGroup.content[0] as InputPopup}.selectedDataValue.toString();
	        	    var inputText = {InputPopupGroup.content[0] as InputPopup}.textInput;
	        	    
	        	    // if Note, update if varname is NOT REDUNDANT
	        	    if (editorPanel.isNoteTile(inputLine) and inputArg==0)
	        	    {
						if (ReservedWords.isReserved(inputText))
	        	        {
	        	            notifyUser("ERROR:  '{inputText}' can't be used as a variable name.  It is a reserved word!");
	        	        }
						else if (vm.isVarNameCandidate(inputText, editorPanel.page) == true)
	        	        {
	        	            notifyUser("ERROR:  The name for the note is already taken!")
	        	        }
	        	        else if (vm.isVarNameCandidate(inputText, editorPanel.page) == false)
	        	        {
	        	            vm.addCandidateVariable(inputText, editorPanel.page);
	        	            editorPanel.updateArgText(inputLine, inputArg, inputText, strDataType, strDataValue, strDataText);
	        	        }
	        	    }
	        	    
	        	    // if not Note, go ahead and update.
	        	    else
	        	    {
	        	    	editorPanel.updateArgText(inputLine, inputArg, inputText, strDataType, strDataValue, strDataText);
	        	    }
	        	    
	        	    
	        	    // if Note, then add to variable list ----------
	        	    // varname:  editorPanel.getNoteType(inputLine);
	        	    // vartype:  editorPanel.getNoteType(inputLine);
	        	    //  1.  Check if it's a note.
	        	    //  2.  Check if all fields are filled.
	        	    
	        	    
	        	    if (editorPanel.isNoteTile(inputLine) and editorPanel.areFieldsFull(inputLine))
	        	    {
	        	        var varname:String = editorPanel.getNoteName(inputLine);
	        	        var vartype:String = editorPanel.getNoteType(inputLine);
						vm.addUserVariable(varname, vartype, editorPanel.page);	        
	        	    }
	        	    /*
	        	    if (editorPanel.isBonusTile(inputLine))
	        	    {
	        	        println("bonus tile!");
						var varnames:String[] = editorPanel.getBonusNames(inputLine);
	        	        var vartypes:String[] = editorPanel.getBonusTypes(inputLine);
	        	        println("gonna add names {varnames}");
	        	        println("gonna add types {vartypes}");
	        	        for (i in [0..varnames.size()])
	        	        {
							vm.addUserVariable(varnames[i], vartypes[i], editorPanel.page);
						}
	        	    }*/
	        	    
	   
	        	    // clean up ----------------------------------
					delete InputPopupGroup.content[0];
					editorPanel.setNumArgClicked(-1);
					inputArg = -1;
	        	    inputLine = -1;
				 	//tranInvis.play();
				 	//tranDarken.stop();
	        	}
	        	
	        }

	    }
	    
	    // -------------------------------------------------
	    // place the tile on the line clicked
	    if (e.primaryButtonDown and inputLine == -1)
	    {
	        // check if there is a tile on the line
	        //println(" check {editorPanel.isLineUsed()}");
	        if (editorPanel.isLineUsed() and editorPanel.isScrolling == false)
	        {
	            // remove tile from line;  store into memory
	            var cn:CustomNode = editorPanel.pickTile();
	            
	            // if we are carrying a tile, then place it down
			    if (TileDragGroupContent.content.size() == 1)
		   	    {
		        	var node      = TileDragGroupContent.content[0];   
		        	delete TileDragGroupContent.content[0];
		        	editorPanel.addTile(node);
		   	    }

	            // drag the tile
 	    		insert cn into TileDragGroupContent.content;
        		tileDragX = e.sceneX as Integer;
        		tileDragY = e.sceneY as Integer;
        		tilePanel.mouseClick = 0;	
	        }
	        
	        // simply place the tile down, if we are dragging one
	        else if (editorPanel.isScrolling == false)
	        {
			    if (TileDragGroupContent.content.size() == 1)
		   	    {
		   	        var line = editorPanel.lineClick;
		        	var node = TileDragGroupContent.content[0];   
		        	delete TileDragGroupContent.content[0];
		        	editorPanel.addTile(node);
		        	
		        	// if the tile that was placed is a bonus tile, then insert bonus vars
		        	/*
	        	    if (editorPanel.isBonusTile(line))
	        	    {
	        	        println("bonus tile!");
						var varnames:String[] = editorPanel.getBonusNames(line);
	        	        var vartypes:String[] = editorPanel.getBonusTypes(line);
	        	        println("gonna add names {varnames}");
	        	        println("gonna add types {vartypes}");
	        	        for (i in [0..varnames.size()])
	        	        {
							vm.addUserVariable(varnames[i], vartypes[i], editorPanel.page);
						}
	        	    }
	        	    */
		   	    }
	        }
	    }
	    
	    // delete the tile on the line clicked
	    else if (e.secondaryButtonDown)
	    {
			vm.delUserVariable(editorPanel.getNoteName(editorPanel.lineClick), editorPanel.page);
        	editorPanel.clearLine();
	    }
	}
}

Stage
{
    title     : "ClueMe Editor"
    width     : screenWidth
    height    : screenHeight
    resizable : false
    
    scene: Scene
    {
        content: bind
        [
        
        	BackgroundGroup,
        	EditorPanelGroup,
        	MenuBarGroup,
        	EventBarGroup,
        	TilePanelGroup,
        	TileDragGroup,
        	ScreenDarkGroup,
        	InputPopupGroup,
        	NotificationGroup,
        	Text
        	{
        	    x: 30
        	    y: 125
        		content: "" //bind "DEBUG INFO:\n ({{eventBar.group.content[1] as SwingComboBox}.selectedItem.value}) {{eventBar.group.content[1] as SwingComboBox}.text}\n ({tilePanel.mode}) MODE= 1:Character, 2:Variable";
        		fill: Color.WHITE;
        	}
        ]
    }
}

function notifyUser(s:String)
{
    {NotificationGroup.content[0] as Notification}.toClose = false;
    tranFadeAway.stop();
    {NotificationGroup.content[0] as Notification}.text = s;
    tranFadeAway.play();
}